home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6986 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: news.voicenet.com!news
  2. From: kobak@voicenet.com (Peter Kobak)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Will it be auto-deleted?
  5. Date: 20 Feb 1996 14:31:58 GMT
  6. Organization: Voicenet - Internet Access - (215)674-9290
  7. Message-ID: <4gcm0u$6ou@news.voicenet.com>
  8. NNTP-Posting-Host: ivyland236.voicenet.com
  9. X-Newsreader: NeoLogic News for OS/2 [version: 4.2]
  10.  
  11. In message <1996Feb20.110314.46035@yogi.urz.unibas.ch> - 
  12. song@iso.iso.unibas.ch (Song Jin) writes:
  13. :>
  14. :>void myfunction(void)
  15. :>{
  16. :> double *mypointer = new double[100];
  17. :>}
  18. :>
  19. :>The mypointer and the buffer it pointed will be auto-deleted after returned
  20. :>from myfunction, am I right?
  21.  
  22. You are wrong. Allocations from 'new' must always be 'delete'd. You could:
  23. 1) manually delete the pointer when you're done with it (not a good choice if 
  24. you've got exceptions), 2) use a "smart" pointer object that knows to delete 
  25. itself (see the FAQ or a decent book for a description of smart pointers), or 
  26. 3) just declare your array directly as an auto variable. If you're not in an 
  27. OS that has problems with stack size, IMHO choice 3 is the best, fastest, and 
  28. simplest.
  29.  
  30. ================
  31. Peter Kobak
  32. kobak@voicenet.com
  33.  
  34.  
  35.